﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using RomHead.model;
using RomHead.viewmodel;

namespace RomHead.view
{
    /// <summary>
    /// Interaction logic for RomDetail.xaml
    /// </summary>
    public partial class RomDetail : Window, RomDetailView
    {
        private SnesRom _rom;
        public SnesRom rom
        {
            get
            {
                return _rom;
            }
            set
            {
                _rom = value;
                romInfoComponent.DataContext = _rom.headerData;
                updateRadioButtonsFromRom();
            }
        }

        public SnesHeaderData header
        {
            get
            {
                return rom.headerData;
            }
        }

        public RomDetail()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            var loadRom = new LoadRomCommand();
            new RomLoadCompleteListener(loadRom, this);
            loadRom.Execute(rom);
        }
        //update radios to reflect the autodetection
        private void updateRadioButtonsFromRom()
        {
            if (false == rom.isHeader && rom.mapper == RomHead.model.SnesRom.MapperTypes.LoROM)
            {
                radioButton1.IsChecked = true;
            }
            else if (true == rom.isHeader && rom.mapper == RomHead.model.SnesRom.MapperTypes.HiROM)
            {
                radioButton2.IsChecked = true;
            }
            else if (true == rom.isHeader && rom.mapper == RomHead.model.SnesRom.MapperTypes.LoROM)
            {
                radioButton3.IsChecked = true;
            }
            else if (false == rom.isHeader && rom.mapper == RomHead.model.SnesRom.MapperTypes.HiROM)
            {
                radioButton4.IsChecked = true;
            }
        }
        private void radioButton1_Checked(object sender, RoutedEventArgs e)
        {
            updateRomLayout(mapper: RomHead.model.SnesRom.MapperTypes.LoROM, isHeader: false);
        }
        private void radioButton2_Checked(object sender, RoutedEventArgs e)
        {
            updateRomLayout(mapper: RomHead.model.SnesRom.MapperTypes.HiROM, isHeader: true);
        }
        private void radioButton3_Checked(object sender, RoutedEventArgs e)
        {
            updateRomLayout(mapper: RomHead.model.SnesRom.MapperTypes.LoROM, isHeader: true);
        }
        private void radioButton4_Checked(object sender, RoutedEventArgs e)
        {
            updateRomLayout(mapper: RomHead.model.SnesRom.MapperTypes.HiROM, isHeader: false);
        }

        private void updateRomLayout(RomHead.model.SnesRom.MapperTypes mapper, bool isHeader)
        {
            if (rom == null) return;
            rom.mapper = mapper;
            rom.isHeader = isHeader;
        }
    }
}
